import { lastValueFrom } from "rxjs";
import { Injectable } from "@nestjs/common";

import {
  Paster,
  PageData,
  Formula,
  ENUM_APP,
  RestDto,
  AssetRpcInterfaceWithDerive,
  AssetRpcGetPasterPath,
  getPasterRpcDto,
  AssetRpcGetFormulaPath,
  AssetRpcPutPasterSyncPath,
  getFormulaRpcDto,
  putPasterSyncRpcDto,
} from "qqlx-core";

import { BusRpc } from "./app.droplet-bus";
import { RpcWaiting } from "./rpc.response";
import { RpcConnection } from "./com.connection";

@Injectable()
export class AssetRpc extends RpcConnection implements AssetRpcInterfaceWithDerive<RestDto> {
  constructor(readonly BusRpc: BusRpc) {
    super(BusRpc, ENUM_APP.POND_ASSET);
  }

  @RpcWaiting(AssetRpcGetPasterPath)
  getPasterByIDs(dto: RestDto & getPasterRpcDto) {
    const ob = this.CLIENT.send(AssetRpcGetPasterPath, dto);
    return lastValueFrom<PageData<Paster>>(ob);
  }

  @RpcWaiting(AssetRpcGetFormulaPath)
  getFormulaByIDs(dto: RestDto & getFormulaRpcDto) {
    const ob = this.CLIENT.send(AssetRpcGetFormulaPath, dto);
    return lastValueFrom<PageData<Formula>>(ob);
  }

  @RpcWaiting(AssetRpcPutPasterSyncPath)
  putPasterSync(dto: RestDto & putPasterSyncRpcDto) {
    const ob = this.CLIENT.send(AssetRpcPutPasterSyncPath, dto);
    return lastValueFrom<Paster>(ob);
  }
}
